<!-- /admin/login.php -->
<?php
session_start();
require_once(__DIR__ . '/../common/db.php');
$msg = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $username = trim($_POST['username']);
    $password = trim($_POST['password']);
    $stmt = $db->prepare("SELECT * FROM admin WHERE username=:u");
    $stmt->bindValue(':u', $username, SQLITE3_TEXT);
    $res = $stmt->execute()->fetchArray(SQLITE3_ASSOC);
    if ($res && password_verify($password, $res['password'])) {
        $_SESSION['admin_user'] = $username;
        $_SESSION['admin_name'] = $res['name'];
        header("Location: /admin/dashboard.php");
        exit;
    } else {
        $msg = "账号或密码错误";
    }
}
?>
<!DOCTYPE html>
<html lang="zh-cn">
<head>
    <meta charset="UTF-8">
    <title>后台登录 - 工会留言板</title>
    <link rel="icon" href="/assets/logo.ico?v=1">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <style>
    body {
        background: #f5f7fa;
        font-family: "微软雅黑", "Microsoft YaHei", Arial, sans-serif;
    }
    .login-box {
        max-width: 400px;
        margin: 100px auto 0;
        background: #fff;
        border-radius: 14px;
        box-shadow: 0 4px 24px #e5eefd;
        padding: 38px 34px 28px;
        display: flex;
        flex-direction: column;
        align-items: stretch;
    }
    .login-title {
        font-size: 1.38em;
        color: #285bb7;
        font-weight: bold;
        margin-bottom: 28px;
        text-align: center;
        letter-spacing: 1px;
    }
    .form-row {
        margin-bottom: 22px;
        position: relative;
        display: flex;
        flex-direction: column;
        align-items: stretch;
    }
    .form-row label {
        color: #333;
        margin-bottom: 7px;
        font-size: 1.07em;
        font-weight: 600;
        letter-spacing: .5px;
        text-align: left;
    }
    .form-row input {
        width: 100%;
        border: 1.6px solid #e2e7f3;
        border-radius: 8px;
        padding: 11px 44px 11px 15px;
        font-size: 1.08em;
        background: #f9fbff;
        box-sizing: border-box;
        transition: border 0.18s;
    }
    .form-row input:focus {
        border-color: #4576f5;
        background: #fff;
        outline: none;
    }
    .input-icon {
        position: absolute;
        right: 16px;
        top: 40px;
        font-size: 1.15em;
        color: #a7bbdf;
        pointer-events: none;
    }
    .form-submit {
        margin-top: 12px;
        text-align: right;
    }
    .form-submit button {
        background: #4576f5;
        color: #fff;
        font-size: 1.13em;
        border: none;
        padding: 10px 0;
        border-radius: 9px;
        cursor: pointer;
        font-weight: 500;
        width: 130px;
        box-shadow: 0 2px 8px #e2ecfa;
        transition: background 0.2s;
    }
    .form-submit button:hover {
        background: #285bb7;
    }
    .tip {
        color: #d94040;
        font-size: 1.09em;
        margin-bottom: 18px;
        text-align: center;
        font-weight: 500;
    }
    @media (max-width: 600px) {
        .login-box { max-width: 97vw; padding: 18vw 4vw; }
        .form-submit button { width: 100%; }
    }
    </style>
    <link rel="stylesheet" href="https://cdn.bootcdn.net/ajax/libs/font-awesome/6.5.0/css/all.min.css">
</head>
<body>
    <div class="login-box">
        <div class="login-title">后台登录</div>
        <?php if($msg) echo "<div class='tip'>" . htmlspecialchars($msg) . "</div>"; ?>
        <form method="post" autocomplete="off">
            <div class="form-row">
                <label for="username">账号</label>
                <input id="username" type="text" name="username" maxlength="20" required placeholder="请输入账号">
                <span class="input-icon"><i class="fa fa-user"></i></span>
            </div>
            <div class="form-row">
                <label for="password">密码</label>
                <input id="password" type="password" name="password" maxlength="32" required placeholder="请输入密码">
                <span class="input-icon"><i class="fa fa-lock"></i></span>
            </div>
            <div class="form-submit">
                <button type="submit">登录</button>
            </div>
        </form>
    </div>
</body>
</html>
